/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.bid.entity.BidPub;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;

/**
 * description:  根据sessionId或者itemId、pubId查询单个BidPub对象
 * @author mhguo 创建时间：2016-4-12  
 * 
 */
public class BidPubEntityPlugin implements PluginConnector {
	private Logger logger = Logger.getLogger(BidPubEntityPlugin.class);
	private IDao bidPubDao;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		try {
			result.setResult("true");
			MethodParam param = null;

			BidPub bidPub = new BidPub();
			bidPub = (BidPub) WbdToolsUtil.getInstance().convert2Bean(vo.getParams(), bidPub);

			if (Validator.isNotNull(bidPub.getPubId())) {
				param = new MethodParam("ById", "", "", BidPub.class.getName());
				param.setInfoId(bidPub.getPubId());
			} else {
				param = new MethodParam("ByBidPubAttr", "", "", BidPub.class.getName());
			}

			if (Validator.isNotNull(bidPub.getItemId()) && !"0".equals(bidPub.getItemId())) {
				param.setParams("itemId", bidPub.getItemId());
			}
			if (Validator.isNotNull(bidPub.getSessionId()) && !"0".equals(bidPub.getSessionId())) {
				param.setParams("sessionId", bidPub.getSessionId());
			}

			if ((!Validator.isEmpty(param.getParams()) && param.getParams().size() > 0)
					|| Validator.isNotNull(param.getInfoId())) {
				BidPub pub = null;
				if (Validator.isNotNull(bidPub.getPubId())) {
					pub = (BidPub) bidPubDao.selectById(param);
				} else {
					pub = (BidPub) bidPubDao.selectSingle(param);
				}
				result.setEntity(pub);
				result.setMessage("查询单个pub对象成功！");
			} else {
				result.setResult("false");
				result.setMessage("缺少必要的参数，查询单个pub对象失败！");
			}
		} catch (Exception e) {
			result.setResult("false");
			logger.error("执行BidPub查询失败 ", e);
			result.setMessage("BidPub查询失败！");
		}

		return result;
	}

	public void setBidPubDao(IDao bidPubDao) {
		this.bidPubDao = bidPubDao;
	}

}
